package GUI;

import Listeners.LoginActionListener;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;

public class LoginPane extends JPanel{

    static JTextField name = new JTextField(10);
    static JTextField id = new JTextField(10);
    JButton login = new JButton("Login");

    public LoginPane(){

        JPanel all = new JPanel(new GridLayout(3,0));
        JPanel namePlace = new JPanel(new GridLayout(0,2));
        JPanel idPlace = new JPanel(new GridLayout(0,2));

        JLabel lname = new JLabel("Name");
        JLabel lid = new JLabel("ID");

        name.setVisible(true);
        id.setVisible(true);

        login.addActionListener(new LoginActionListener());

        namePlace.add(lname);
        namePlace.add(name);

        idPlace.add(lid);
        idPlace.add(id);

        all.add(namePlace);
        all.add(idPlace);
        all.add(login);

        this.add(all);
    }

    public static void valuesEntered(){

        if(name.getText().equals("") || id.getText().equals("")){
            JOptionPane.showMessageDialog(null, "ID or Name field has not been filled in");
        }
        else{

            try{
                String response = MainFrame.communicate.sendData(id.getText(),name.getText());
                processMessage(response);

            }catch(IOException e){
                e.printStackTrace();
            }

        }
    }
    private static void processMessage(String message){

        char current;

        current = message.charAt(5);

        if(current == 'T'){
            MainFrame.updateStatus("Logged In");
        }
        else{
            MainFrame.updateStatus("User Does Not Exist: Try Again");
        }

    }
}
